package com.hbt.android.kotlin

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.util.Log
import android.view.Gravity
import android.widget.TextView
import com.google.android.material.bottomnavigation.BottomNavigationView
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import androidx.navigation.ui.setupWithNavController
import com.google.android.material.button.MaterialButtonToggleGroup
import com.hbt.android.kotlin.databinding.ActivityMainBinding
import com.hbt.android.kotlin.http.OkHttp
import org.w3c.dom.Text
import java.util.*

class MainActivity : AppCompatActivity() {

private lateinit var binding: ActivityMainBinding
private lateinit var textView: TextView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        // 原始页面
//        binding = ActivityMainBinding.inflate(layoutInflater)
//        setContentView(binding.root)
//        val navView: BottomNavigationView = binding.navView
//        val navController = findNavController(R.id.nav_host_fragment_activity_main)
//        // Passing each menu ID as a set of Ids because each
//        // menu should be considered as top level destinations.
//        val appBarConfiguration = AppBarConfiguration(setOf(
//            R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications))
//        setupActionBarWithNavController(navController, appBarConfiguration)
//        navView.setupWithNavController(navController)

        // 课程页面
        setContentView(R.layout.activity_main)
        val navView: BottomNavigationView = findViewById(R.id.nav_view)
        val navController = findNavController(R.id.nav_host_fragment_activity_main)
        navView.setupWithNavController(navController)

        // activity测试
//        textView = TextView(this)
//        textView.text = "MainActivity"
//        textView.gravity = Gravity.CENTER
//        setContentView(textView)


        /* 显式启动 ，通过包名启动 ，与其他Activity交互*/
        // 常规跳转
//        textView.setOnClickListener {
//            val intent = Intent(MainActivity@this, LearnActivity::class.java)
//            startActivity(intent)
//        }

        // 携带参数跳转
//        textView.setOnClickListener {
//            val intent = Intent(MainActivity@this, LearnActivity::class.java)
//            intent.putExtra("extra_data", "extra_data")
//            intent.putExtra("extra_data1", 1000)
//            startActivity(intent)
//        }

        // 期待从目标页面获取数据，startActivityForResult ，比如启动相册获取图片
//        textView.setOnClickListener {
//            val intent = Intent(MainActivity@this, LearnActivity::class.java)
//            intent.putExtra("request_code", 1000)
//            intent.putExtra("request_msg", "request")
//            startActivityForResult(intent, 1000)
//        }


        /* 隐式启动， 通过指定category和action信息， 让系统分析Intent，找出合适的activity去启动 */
//        textView.setOnClickListener {
//            val intent = Intent()
//            // 和AndroidManifest.xml中的app下的action保持一致
//            intent.action = "com.hbt.android.kotlin.action.LEARNACTIVITY"
//            intent.addCategory("com.hbt.android.kotlin.category.LEARNACTIVITY")
//            intent.putExtra("request_code", 200)
//            intent.putExtra("request_msg", "request")
//            startActivity(intent)
//        }

        /* 触发打电话 */
//        val uri: Uri = Uri.parse("tel:10086")
//        val intent = Intent(Intent.ACTION_DIAL, uri)
//        startActivity(intent)

        /* 发短信 */
//        val uri: Uri = Uri.parse("smsto:10086")
//        val intent = Intent(Intent.ACTION_SENDTO, uri)
//        intent.putExtra("sms_body", "hello 10086")
//        startActivity(intent)

        /* 打开浏览器 */
//        val uri: Uri = Uri.parse("http://www.baidu.com")
//        val intent = Intent(Intent.ACTION_VIEW, uri)
//        intent.putExtra("keyword", "哔哩哔哩")
//        startActivity(intent)

        /* 多媒体播放 */
//        val intent = Intent(Intent.ACTION_VIEW)
//        val uri: Uri = Uri.parse("file:////sdcard/foo.mp3")
//        intent.setDataAndType(uri, "audio/mp3")
//        startActivity(intent)

        /* 打开摄像头拍照 */
//        val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
//        startActivityForResult(intent, 1001)



        Log.e("MainActivity", "onCreate")
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)



        if (requestCode == 1000 && resultCode == Activity.RESULT_OK && data != null) {
            val response_code = data.getIntExtra("response_code", 0)
            val response_msg = data.getStringExtra("response_msg")
            textView.text = "${response_code}, ${response_msg}"
        }
        Log.e("MainActivity", "onActivityResult")

    }

    override fun onStart() {
        super.onStart()
        Log.e("MainActivity", "onStart")
    }

    override fun onResume() {
        super.onResume()
        Log.e("MainActivity", "onResume")
    }

    override fun onPause() {
        super.onPause()
        Log.e("MainActivity", "onPause")
    }

    override fun onStop() {
        super.onStop()
        Log.e("MainActivity", "onStop")
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e("MainActivity", "onDestroy")
    }

    override fun onRestart() {
        super.onRestart()
        Log.e("MainActivity", "onRestart")
    }
}